const http = require("http");
const fs = require("fs");

const wait = (seconds) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, seconds * 1000);
  });
};
http
  .createServer(function (request, response) {
    console.log("request come", request.url);
    console.log("host", request.headers.host);
    if (request.url === "/") {
      const html = fs.readFileSync("test.html", "utf-8");
      response.writeHead(200, {
        "Content-Type": "text/html",
      });
      response.end(html);
    }
    if (request.url === "/data") {
      response.writeHead(200, {
        "Cache-Control": "s-maxage=200", // 给资源设置缓存时间
        Vary: "X-Text-Cache", // 只有当浏览器的请求头中 "X-Text-Cache" 的值一样时才进行数据缓存
      });
      wait(2).then(() => response.end("success"));
    }
  })
  .listen(8888);

console.log("server is listening on 8888");
